/*
 * @文件描述:继电器控制
 * @版本:
 * @作者:周晨阳
 * @Date:2021-11-19 16:09:26
 */
#include "Relay.h"
#if BOARD_INUSE == BOARD_STM32

#include "stm32f10x.h"
static RELAY_STATE RelayStateList[5] = {0};
static uint16 channel2GPIOMap[5] = {GPIO_Pin_0, GPIO_Pin_1, GPIO_Pin_2, GPIO_Pin_3};

/**
 * @brief 继电器IO初始化，使用GPIOA 0，1，2，3作为 ，ON/OFF ，- ， + ， MODE，4个按键
 * 
 */
void relayInit()
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能ADC1通道时钟

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**
 * @brief Get the Relay State 
 * 
 * @param channel 继电器id
 * @return RELAY_STATE 
 */
RELAY_STATE getRelayState(u8 channel)
{
    return RelayStateList[channel];
}
/**
 * @brief Set the Relay 
 * 
 * @param state 继电器状态
 */
void setRelay(u8 channel, RELAY_STATE state)
{
    if (channel > MY_RELAY_COUNT)
        return;
    if (state == RELAY_STATE_ON)
    {
        GPIO_SetBits(GPIOA, channel2GPIOMap[channel]); 
    }
    else
    {
        GPIO_ResetBits(GPIOA, channel2GPIOMap[channel]); 
    }

    RelayStateList[channel] = state;
}

/**
 * @brief 重置所有继电器
 * 
 */
void resetRelay(void)
{

    u8 i = 0;
    for (i = 0; i < MY_RELAY_COUNT; i++)
    {
        setRelay(i, RELAY_STATE_OFF);
    }
}
#endif